// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods, numericLiterals
/*
 * UserEditPanel.java
 */
package edu.gatech.oad.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import edu.gatech.oad.records.PatientHistoryPanel;
import edu.gatech.oad.ui.UserPanel;

public class UserEditPanel extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private User editUser;

	private User currentUser;
	
	private JTextField emailField;
	
	private JTextField phoneField;
	
	private JPasswordField passwordField;
	
	private JPasswordField confirmPasswordField;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	private JTextField firstNameField;
	
	private JLabel userNameLabel;
	
	private JTextField lastNameField;
	
	private JRadioButton maleRadioButton;
	
	private JRadioButton femaleRadioButton;
	
	private JCheckBox chckbxSuspended = null;
	
	private UserPanel userPanel;
	
	private boolean isRegistering = false;
	
	/**
	 * Create the panel.
	 */
	private UserEditPanel() {
		setLayout(null);
		
		userNameLabel = new JLabel("Username");
		userNameLabel.setBounds(8, 5, 510, 16);
		add(userNameLabel);
		
		final JLabel lblName = new JLabel("First Name");
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setBounds(54, 51, 68, 16);
		add(lblName);
		
		final JLabel lblNewLabel = new JLabel("Email Address");
		lblNewLabel.setBounds(33, 117, 89, 16);
		add(lblNewLabel);
		
		emailField = new JTextField();
		emailField.setBounds(130, 111, 394, 28);
		add(emailField);
		emailField.setColumns(10);
		
		final JLabel lblPhoneNumber = new JLabel("Phone Number");
		lblPhoneNumber.setBounds(30, 150, 92, 16);
		add(lblPhoneNumber);
		
		phoneField = new JTextField();
		phoneField.setBounds(130, 144, 394, 28);
		add(phoneField);
		phoneField.setColumns(10);
		
		final JLabel lblSetPassword = new JLabel("Set Password");
		lblSetPassword.setBounds(40, 183, 82, 16);
		add(lblSetPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(130, 177, 394, 28);
		add(passwordField);
		
		final JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setBounds(8, 216, 114, 16);
		add(lblConfirmPassword);
		
		confirmPasswordField = new JPasswordField();
		confirmPasswordField.setBounds(130, 210, 394, 28);
		add(confirmPasswordField);
		
		maleRadioButton = new JRadioButton("Male");
		buttonGroup.add(maleRadioButton);
		maleRadioButton.setBounds(130, 250, 61, 23);
		maleRadioButton.getModel().setActionCommand("Male");
		add(maleRadioButton);
		
		femaleRadioButton = new JRadioButton("Female");
		buttonGroup.add(femaleRadioButton);
		femaleRadioButton.setBounds(203, 250, 76, 23);
		femaleRadioButton.getModel().setActionCommand("Female");
		add(femaleRadioButton);
		
		final JLabel lblSex = new JLabel("Sex");
		lblSex.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSex.setBounds(61, 254, 61, 16);
		add(lblSex);
		
		final JButton btnSave = new JButton("Save");
		btnSave.setBounds(401, 296, 117, 29);
		btnSave.addActionListener(this);
		add(btnSave);
		
		firstNameField = new JTextField();
		firstNameField.setBounds(130, 45, 394, 28);
		add(firstNameField);
		firstNameField.setColumns(10);
		
		final JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLastName.setBounds(54, 85, 68, 16);
		add(lblLastName);
		
		lastNameField = new JTextField();
		lastNameField.setColumns(10);
		lastNameField.setBounds(130, 79, 394, 28);
		add(lastNameField);

	}
	
	public UserEditPanel(User editUser, User currentUser, UserPanel userPanel) {
		this();
		this.editUser = editUser;
		this.currentUser = currentUser;
		
		this.userPanel = userPanel;
				
		if (editUser instanceof Patient) {
			final JButton btnViewHistory = new JButton("View Patient History");
			btnViewHistory.setBounds(5, 296, 150, 29);
			btnViewHistory.addActionListener(this);
			//add(btnViewHistory);
		}
		else if (editUser instanceof MedicalPersonnel) {
			final JButton btnViewSchedule = new JButton("View Schedule");
			btnViewSchedule.setBounds(5, 296, 150, 29);
			//add(btnViewSchedule);
		}
		
		if (currentUser instanceof SystemAdministrator &&
				!(editUser instanceof SystemAdministrator)) {
			chckbxSuspended = new JCheckBox("Suspended");
			chckbxSuspended.setBounds(390, 10, 128, 23);
			chckbxSuspended.setSelected(editUser.isSuspended());
			add(chckbxSuspended);
		}
		
		userNameLabel.setText("Edit user " + editUser.getUsername());
		firstNameField.setText(this.editUser.getFirstName());
		lastNameField.setText(this.editUser.getLastName());
		emailField.setText(this.editUser.getEmailAddress());
		phoneField.setText(this.editUser.getPhoneNumber());
		
		if (this.editUser.getIsMale()) {
			buttonGroup.setSelected(maleRadioButton.getModel(), true);
		}
		else {
			buttonGroup.setSelected(femaleRadioButton.getModel(), true);
		}
	}
	
	public UserEditPanel(boolean registerPanel, User user){
		this();
		isRegistering = true;
		if(user != null){
			if(user instanceof SystemAdministrator){
				addPersonnelRadioButtons();
				addSystemAdministratorButtons();
			}
			if(user instanceof MedicalPersonnel){
				addPersonnelRadioButtons();
			}
			currentUser = user;
		}
	}

	public UserEditPanel(boolean registerPanel){
		this();
	}
	
	public void addPersonnelRadioButtons(){
	
	}
	
	public void addSystemAdministratorButtons(){
		
	}
	
	
	public void actionPerformed(ActionEvent e) {
		final String actionCommand = e.getActionCommand();
				
		if (actionCommand.equals("Save")) {
			final String password = new String(passwordField.getPassword());
			final String confirmPassword = new String(confirmPasswordField.getPassword());
			
			if(isRegistering){
				
			}
			if (password.equals(confirmPassword)) {
				if (passwordField.getPassword().length > 0) {
					editUser.setPassword(password);
				}
			}
			else {
				JOptionPane.showMessageDialog(this,
					"The passwords you entered do not match. Please reenter your new password." // $codepro.audit.disable lineLength
					);
			}
			
			passwordField.setText("");
			confirmPasswordField.setText("");
			
			editUser.setFirstName(firstNameField.getText());
			editUser.setLastName(lastNameField.getText());
			editUser.setEmailAddress(emailField.getText());
			editUser.setPhoneNumber(phoneField.getText());
			
			final ButtonModel model = buttonGroup.getSelection();
			
			if (model.getActionCommand().equals("Male")) {
				editUser.setIsMale(true);
			}
			else {
				editUser.setIsMale(false);
			}
			
			if (chckbxSuspended != null) {
				editUser.setSuspended(chckbxSuspended.isSelected());
			}
		}
		else if (actionCommand.equals("View Patient History")) {
			assert(editUser instanceof Patient);
			final Patient patient = (Patient) editUser;
			
			final PatientHistoryPanel historyPanel = new PatientHistoryPanel(
					patient.getPatientHistory(),
					currentUser, this.userPanel);
			this.userPanel.addNewTab("History", historyPanel);
		}
	}
}
